package com.lill.stock.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lill.common.component.util.IdUtil;
import com.lill.product.model.StorageInfoModel;
import com.lill.product.service.IStorageInfoService;
import com.lill.stock.dao.IStockRealDao;
import com.lill.stock.dao.IStockTransferDao;
import com.lill.stock.model.StockInnerDetailModel;
import com.lill.stock.model.StockInnerModel;
import com.lill.stock.model.StockRealModel;
import com.lill.stock.model.StockTransferModel;
import com.lill.stock.service.IStockTransferService;

/**
 * 车辆调拨Service实现类
 *
 * @author lill
 * @date 2021-11-08 22:37:11
 */
@Service("com.lill.stock.service.impl.StockTransferServiceImpl")
public class StockTransferServiceImpl implements IStockTransferService {
	
    @Autowired
    private IStockTransferDao stockTransferDao;
    @Autowired
    private IStockRealDao stockRealDao;
    @Autowired
    private IStorageInfoService storageInfoService;

    public StockTransferModel queryObject(String id) {
        return stockTransferDao.queryObject(id);
    }

    public List<StockTransferModel> queryList(Map<String, Object> map) {
        return stockTransferDao.queryList(map);
    }

    public int queryTotal(Map<String, Object> map) {
        return stockTransferDao.queryTotal(map);
    }

    public int save(StockTransferModel stockTransfer) {
        stockTransfer.setId(IdUtil.createIdbyUUID());
        return stockTransferDao.save(stockTransfer);
    }
    
    public int saveBatch(StockInnerModel stockInner, String personId) {
    	int result=0;
    	if(CollectionUtils.isNotEmpty(stockInner.getProductList())&&StringUtils.isNotEmpty(stockInner.getStorageId())){
    		StorageInfoModel storageInfo=storageInfoService.queryObject(stockInner.getStorageId());
    		if(null!=storageInfo){
    			Date nowdate=new Date();
    			for (StockInnerDetailModel innerDetail : stockInner.getProductList()) {
    				StockTransferModel stockTransfer=new StockTransferModel();
    				stockTransfer.setId(IdUtil.createIdbyUUID());
    				stockTransfer.setTargetBranchId(storageInfo.getBranchId());
    				stockTransfer.setTargetStorageId(stockInner.getStorageId());
    				StockRealModel stockReal=stockRealDao.queryObject(innerDetail.getId());
    				stockTransfer.setCompanyId(stockReal.getCompanyId());
    				stockTransfer.setSourceBranchId(stockReal.getBranchId());
    				stockTransfer.setSourceStorageId(stockReal.getStorageId());
    				stockTransfer.setSourceStorageId(stockReal.getStorageId());
    				stockTransfer.setPersonId(personId);
    				stockTransfer.setTransferDate(nowdate);
    				stockTransfer.setContent(stockInner.getContent());
    				stockTransfer.setStockRealId(innerDetail.getId());
    				
    				stockTransferDao.save(stockTransfer);
    				stockReal.setBranchId(storageInfo.getBranchId());
    				stockReal.setStorageId(stockInner.getStorageId());
    				result+=stockRealDao.update(stockReal);
    			}
    		}
    	}
		return result;
	}

    public int update(StockTransferModel stockTransfer) {
        return stockTransferDao.update(stockTransfer);
    }

    public int delete(String id) {
        return stockTransferDao.delete(id);
    }

    public int deleteBatch(String[] ids) {
        return stockTransferDao.deleteBatch(ids);
    }
    
	public int deleteLogicByMap(Map<String, Object> map) {
		return stockTransferDao.deleteLogicByMap(map);
	}

}
